package com.itheima.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.boot.domain.Book;
import com.itheima.boot.domain.R;
import com.itheima.boot.mapper.BookMapper;
import com.itheima.boot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    /**
     * 条件查询
     * @param book
     * @return
     */
    @Override
    public Book selectByCondition(Book book) {

       LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
        lqw.eq(book.getBookName() != null,Book::getBookName,book.getBookName());
        lqw.eq(book.getStatus() != null,Book::getStatus,book.getStatus());

        return bookMapper.selectOne(lqw);
    }


    /**
     * 添加
     * @param book
     * @return
     */
    @Override
    public boolean addBook(Book book) {

        int insert = bookMapper.insert(book);
        return insert != 0;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {

        int index = bookMapper.deleteById(id);
        return index != 0;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Book selectById(Long id) {
        return bookMapper.selectById(id);
    }

    /**
     * 修改
     * @param book
     * @return
     */
    @Override
    public boolean updateBook(Book book) {

        int index = bookMapper.updateById(book);
        return index != 0;
    }
}
